const express = require('express');
const bcrypt = require('bcryptjs')
const response = require('../../utils/response');
const task = require('../../models/task');
const taskaudit = require('../../models/taskaudit');
const router = express.Router();

//获取审核列表
router.get('/getExamine', async (req, res) => {
  const status = Number(req.query.status)
  const pagenum = Number(req.query.pagenum)
  const pagesize = Number(req.query.pagesize)
  var query = String(req.query.query)
  if(query == 'undefined') {
    var query = ''
  }
  const data = await task.find({
      status,
      taskname:{$regex:query}
    })
    .skip(pagesize * (pagenum - 1))
    .limit(pagesize)
    .populate('worker',['name'])
    .populate('creator',['name'])
    .sort({createdAt:-1})
    .lean()

  const total = await task.find({
    status
  }).countDocuments()


  response(res, 200, '获取任务列表成功', {
    data,
    total,
    pagenum
  })
})

//获取审核记录列表
router.get('/historyExamines', async (req, res) => {
  const pagenum = Number(req.query.pagenum)
  const pagesize = Number(req.query.pagesize)
  var query = String(req.query.query)
  if(query == 'undefined') {
    var query = ''
  }
  const data = await taskaudit.find({
      taskname:{$regex:query}
    })
    .populate('reviewer',['name'])
    .skip(pagesize * (pagenum - 1))
    .limit(pagesize)
    .sort({createdAt:-1})
    .lean()
  const total = await taskaudit.find().countDocuments()
  response(res, 200, '获取任务列表成功', {
    data,
    total,
    pagenum
  })
})


//提交审核
router.put('/submitExamine/:taskId', async (req, res) => {
  const taskId = req.params.taskId
  const data = await task.findById(taskId)
    .populate('creator',['name'])
    .populate('worker',['name'])
  if(!data) {
    response(res, 404, '没有找到该任务', {
      data,
    })
  }
  data.status = 1
  let model = await data.save()
  response(res, 200, '获取任务列表成功', {
    model,
  })
})

//退回审核
router.put('/returnExamine/:taskId', async (req, res) => {
  const taskId = req.params.taskId
  const data = await task.findById(taskId)
    .populate('creator',['name'])
    .populate('worker',['name'])
  if(!data) {
    response(res, 404, '没有找到该任务', {
      data,
    })
  }
  data.status = 2
  let model = await data.save()
  response(res, 200, '获取任务列表成功', {
    model,
  })
})



//完成审核
router.put('/completeExamine/:taskId', async (req, res) => {
  const taskId = req.params.taskId
  const data = await task.findById(taskId)
    .populate('creator',['name'])
    .populate('worker',['name'])
  if(!data) {
    response(res, 404, '没有找到该任务', {
      data,
    })
  }
  data.status = 3
  let model = await data.save()
  response(res, 200, '获取任务列表成功', {
    model,
  })
})



//查看任务详情
router.get('/seeExamine/:taskId', async (req, res) => {
  const taskId = req.params.taskId
  const data = await task.findById(taskId)
    .populate('creator',['name'])
    .populate('worker',['name'])
    .lean()
  if(!data) {
    response(res, 404, '没有找到该任务', {
      data,
    })
  }
  response(res, 200, '获取任务详情成功', {
    data,
  })
})

//删除任务提交记录
router.delete('/recordDel/:taskId', async (req, res) => {
  const taskId = req.params.taskId
  const id = req.body.id
  const data = await task.findById(taskId)
    .populate('creator',['name'])
    .populate('worker',['name'])
  if(!data) {
    response(res, 404, '没有找到该任务', {
      data,
    })
  }
  for(key in data.fileAddress){
    if(id == data.fileAddress[key]._id) {
      data.fileAddress.splice(key,1)
    }
  }
  await data.save()
  response(res, 200, '删除记录成功', {
    data,
  })
})



module.exports = router;